package vip.source.pattern.part06.improve.adapter;

import com.alibaba.fastjson.JSON;
import vip.source.pattern.part06.improve.domain.RebateInfo;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class MQAdapter {

    /** 返回统一的MQ消息体 */
    public static RebateInfo filter(String strJson, Map<String, String> link)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        return filter(JSON.parseObject(strJson, Map.class), link);
    }

    public static RebateInfo filter(Map obj, Map<String, String> link)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        RebateInfo rebateInfo = new RebateInfo();
        for (String key : link.keySet()) {
            Object val = obj.get(link.get(key));
            RebateInfo.class
                    .getMethod(
                            "set" + key.substring(0, 1).toUpperCase() + key.substring(1),
                            String.class)
                    .invoke(rebateInfo, val.toString());
        }
        return rebateInfo;
    }
}
